package com.category.douyijia.services;

import java.util.List;

import com.category.douyijia.base.BaseService;
import com.category.douyijia.model.User;
import com.category.douyijia.utils.Encryption;
/**
 * user service management.
 * it include query,
 * insert,update and delete etc 
 * base methods.
 * and user login,
 * user identity verification,
 * user state update,
 * and user password reset.
 * @author JunfengLiu
 * @version v1.0
 * date time:2021-7-19 16:17:27
 */
public class UserService extends BaseService<UserService, User> {
	/**
	 * user dao factory.
	 */
	public static User userDao = (User)new User().dao();
	/**
	 * user login checked.
	 * @param userId
	 * user login id.
	 * @return
	 * return the user model message.
	 */
	//通过id查找
	public static User userLogin(String userId){
		return userDao.findById(userId);
	}
	/**
	 * find all user message.
	 * @return
	 * return the list of user message.
	 */
	//查找全部
	public static List<User> findAll(){
		return userDao.findAll();
	}
	/**
	 * user message add methods.
	 * @param user
	 * user message.
	 * @return
	 * save result.
	 */
	//添加
	public static boolean insert(User user){
		if (user != null) {
			return user.dao().save();
		}else {
			return false;
		}
	}
	/**
	 * update user message
	 * by user login id.
	 * @param userId
	 * user login id
	 * @param newUser
	 * the new user message.
	 * @return
	 * update state
	 */
	//修改
	public static boolean update(String userId,User newUser){
		
		if (newUser != null) {
			return userDao.findById(userId)._setAttrs(newUser).update();
		}else {
			return false;
		}
	}
	/**
	 * delete user by user id.
	 * @param userId
	 * user login id.
	 * @return
	 * delete state.
	 */
	//删除
	public static boolean deleteById(String userId){
		if (userId != null && !(userId.equals(""))) {
			return userDao.findById(userId).delete();
		}
		return false;
	}
	/**
	 * find user message by
	 * user login id.
	 * @param userId
	 * user login id.
	 * @return
	 * user message.
	 */
	//查找
	public static User findUserById(String userId){
		if (userId != null && !(userId.equals(""))) {
			return userDao.findById(userId);
		}
		return null;
	}
	/**
	 * find user or user list
	 * by user name.
	 * @param userName
	 * user name.
	 * @return
	 * user list
	 */
	//通过姓名查找用户信息
	public static List<User> findUserByName(String userName){
		if (userName != null && !(userName.equals(""))) {
			return userDao.find("select * from t_user "
								+ "where user_name =?"
								, userName);
		}else {
			return null;
		}
	}
	/**
	 * find user by user nick name.
	 * @param userNickName
	 * user nick name
	 * @return
	 * user or user list.
	 */
	//通过昵称查找
	public static List<User> findUserByNickname(String userNickName){
		if (userNickName != null && !(userNickName.equals(""))) {
			return userDao.find("select * from t_user "
								+ "where user_name =?"
								, userNickName);
		}else{
			return null;
		}
	}
	/**
	 * find user by user identity number.
	 * @param userIdentityId
	 * user identity number.
	 * @return
	 * user list.
	 */
	//通过身份证号查找
	public static List<User> findUserByUserIdentityId(String userIdentityId){
		if (userIdentityId.length() == 18) {
			return userDao.find("select * from t_user "
								+ "where user_identity =?"
								, userIdentityId);
		}else {
			return null;
		}
	}
	/**
	 * reset all user password
	 * default '123456'
	 * @return
	 * result state
	 */
	//重置所有密码
	public static boolean resetAllUserPassword(){
		try {
			return userDao.set("userPassword"
								, Encryption.passwordMD5("123456"))
								.update();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	/**
	 * reset user password
	 * by user login id.
	 * @param userId
	 * user login id.
	 * @return
	 * reset result.
	 */
	//通过登录id查找账号
	public static boolean resetUserPasswordByUserId(String userId){
		try {
			return findUserById(userId).set("userPassword"
									, Encryption.passwordMD5("123456"))
									.update();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	/**
	 * stop user state.
	 * @param userId
	 * user login id.
	 * @return
	 * stop result.
	 */
	//停用账户
	public static boolean stopUserUsingById(String userId){
		if (userId != null && !(userId.equals(""))) {
			return findUserById(userId).set("userStatue", 0).update();
		}else{
			return false;
		}
	}
}